<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing:  Language Translater
 *
 * @category   	Gnn Cms
 * @package    	Gnn Language
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 Jul 2, 2013 || 10:17:41 AM
 */
class Zendvn_Translate{
	
	public  $_data = array();
	public $config;
	public $gcLocale;
	
	public $gcTemplate;
	public $gcComponent;
	
	public $gcLanguage;
	
	public function __construct(Zendvn_Controller_Action $action){
		$this->gcTemplate 	= $action->gcTemplate;
		
		$this->gcComponent 	= $action->gcComponent;
		
		$locale 			= Zend_Registry::get(REGISTRY_KEY_LOCALE);
		$this->gcLanguage 	= $locale->getLanguage();
		
		
		$this->getData();
		
// 		echo "<pre>";
// 		print_r($this->_data);
// 		echo "</pre>";
// 		die();
		$options = array('adapter'=>'Array','content'=>$this->_data ,'locale'=>$this->gcLanguage);
		$trans = new Zend_Translate($options);
		
		Zend_Registry::set("Zend_Translate", $trans);
	}
	
	/**
	 * @return the languages (array)
	 */
	public function getData(){
		$cache = Zendvn_Cache::getOtherCacher();
		$temLang = Zendvn_Cache::getVariableFromCache($cache,'temLang_'.$this->gcLanguage.'_'.$this->gcTemplate->dbInfo->template,$this,'loadTemplateLanguage');
		$modLang = Zendvn_Cache::getVariableFromCache($cache,'modLang_'.$this->gcLanguage.'_'.$this->gcTemplate->dbInfo->client_id,$this,'loadModuleLanguage');
		$comLang = Zendvn_Cache::getVariableFromCache($cache,'comLang_'.$this->gcLanguage.'_'.$this->gcTemplate->dbInfo->client_id,$this,'loadComponentLanguage');

		$this->_data = array_merge($this->_data,$temLang);
		$this->_data = array_merge($this->_data,$modLang);
		$this->_data = array_merge($this->_data,$comLang);
	}
	
	/**
	 * read xml file and return it to an array
	 * @param  String $fileConfigs
	 * @return Array
	 */
	public function addFile($fileConfigs = null){
		$data 		= array();
		if(file_exists($fileConfigs)){
			$data 	= array();
			$config = new Zendvn_Config($fileConfigs,$this->gcLanguage,'xml');
			$word 	= $config->getConfig('word');
			
			if(count($word) == 2 && !isset($word[1])){
				$data[$word['alias']] = $word['value'];
			}
			else if (count($word) >0){
				foreach ($word as $value){
					$data[$value['alias']] = $value['value'];
				}
			}
		}
		return $data;
	} 
	
	/**
	 * 
	 * Load template language
	 * @return Array
	 */
	final public function loadTemplateLanguage(){
		$languageFilePath = $this->gcTemplate->templatePath .DS.'languages'.DS.$this->gcLanguage.'.xml'; 
		return $this->addFile($languageFilePath);
	}
	
	/**
	 * 
	 * Load component language
	 * @return Array
	 */
	public function loadComponentLanguage(){
		$comFilePath = $this->gcComponent->gcComponentPath .DS.'languages'.DS.$this->gcLanguage.'.xml';
		return $this->addFile($comFilePath);
		
	}
	
	/**
	 * 
	 * Load all modules's language
	 * @return Array
	 */
	final public function loadModuleLanguage(){
		$data 	= array();
		$db 	= Zend_Registry::get(REGISTRY_KEY_DBO);
		$sql 	= $db   ->select()
						->from(TBL_PRF.'modules',array('module'))
						->where('client_id = ?',$this->gcTemplate->dbInfo->client_id);
		$list 	= $db->fetchCol($sql);
		
		//add file for each module
		foreach ($list as $module){
			$modulePath = ($this->gcTemplate->dbInfo->client_id == 1) ? MODULES_PATH_FRONT : MODULES_PATH_BACK;
			$fileConfigs = $modulePath.DS.$module.DS.'languages'.DS.$this->gcLanguage.'.xml';
			$data = array_merge($data,$this->addFile($fileConfigs));
		}
		return $data;
	}
}